package gui.help;

import common.Constants;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * Displays the product information to the user.
 * @author William Whitney
 */
public class AboutBox extends JFrame
{
    private static final long serialVersionUID = 1L;

    /** Holds the product information JPanel */
    private JPanel prodPanel;
    /** Font Size */
    public static final int kHeadFontSize = 11;
    /** Rows */
    public static final int kNumRows = 5;
    /** Rows */
    public static final int kNumRows3 = 3;
    private final AboutBoxInfo aboutInfo;

    /**
     * Default constructor.
     */
    public AboutBox()
    {
        this.setTitle("About");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //CREATE layout to hold a centered FlowLayout
        FlowLayout layout = new FlowLayout(FlowLayout.CENTER);
        this.setLayout(layout);
        this.setResizable(false);
        this.aboutInfo = new AboutBoxInfo();
    }

    /**
     * Displays the product info to the user.
     */
    public void showUI()
    {
        if (this.prodPanel != null)
        {
            this.remove(prodPanel);
        }

        this.prodPanel = getProductInfoPanel();
        this.add(prodPanel);
        pack();
        this.setVisible(true);
    }

    /**
     * Builds the current product info.
     */
    private JPanel getProductInfoPanel()
    {
        //CREATE panel to hold the panel returned
        JPanel panel = new JPanel();
        //CREATE layout to hold a grid layout
        BoxLayout layout = new BoxLayout(panel, BoxLayout.Y_AXIS);
        panel.setLayout(layout);
        panel.add(getPanelHead());
        panel.add(getPanelBody());
        addCloseButton(panel);

        return panel;
    }

    /**
     * Attaches title and description to the passed in panel.
     * @returns a panel with the title and description
     */
    private JPanel getPanelHead()
    {
        //CREATE myPanel to hold returned JPanel
        JPanel myPanel = new JPanel();
        //CREATE GridLayout to hold current layout
        GridLayout gLayout = new GridLayout(2, 2);
        myPanel.setLayout(gLayout);
        //CREATE title for title
        JLabel title = new JLabel(aboutInfo.getTitle());
        title.setFont(Constants.getHeadingFont());
        title.setHorizontalAlignment(JLabel.LEFT);
        //CREATE description
        JLabel description = new JLabel(aboutInfo.getDescription());
        description.setHorizontalAlignment(JLabel.LEFT);

        myPanel.add(title);
        myPanel.add(description);
        return myPanel;
    }

    /**
     * Returns a JPanel with the product version, vendor and homepage.
     * @return JPanel with version, vendor and homepage
     */
    private JPanel getPanelBody()
    {
        //CREATE myPanel to hold returned JPanel
        JPanel myPanel = new JPanel();
        //CREATE GridLayout to hold current layout
        GridLayout gLayout = new GridLayout(kNumRows3, 2);
        myPanel.setLayout(gLayout);
        //CREATE headFont to hold the current font
        Font headFont = new Font("Tahoma", Font.BOLD, kHeadFontSize);
        //CREATE versionHead for the current product version
        JLabel versionHead = new JLabel("Product Version: ");
        versionHead.setFont(headFont);
        versionHead.setHorizontalAlignment(JLabel.LEFT);
        myPanel.add(versionHead);
        //CREATE version to hold the current version
        JLabel version = new JLabel(aboutInfo.getVersion());
        version.setHorizontalAlignment(JLabel.LEFT);
        myPanel.add(version);

        //CREATE homePageHead for current homepage
        JLabel homePageHead = new JLabel("Homepage: ");
        homePageHead.setFont(headFont);
        homePageHead.setHorizontalAlignment(JLabel.LEFT);
        myPanel.add(homePageHead);

        //CREATE homePage for current homepage
        JLabel homePage = new JLabel(aboutInfo.getHomepage());
        homePage.setForeground(Color.BLUE);
        homePage.setHorizontalAlignment(JLabel.LEFT);
        myPanel.add(homePage);
        homePage.addMouseListener(new MouseAdapter()
        {

            @Override
            public void mouseClicked(MouseEvent me)
            {
                URI uri = URI.create(aboutInfo.getHomepage());
                try
                {
                    Desktop.getDesktop().browse(uri);
                }
                catch (IOException ex)
                {
                    Logger.getLogger(AboutBox.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        });

        return myPanel;
    }

    /**
     * Attaches a close button to the passed in panel.
     * @param panel to attach close button to.
     */
    private void addCloseButton(JPanel panel)
    {
        //CREATE closeBtn for the close button
        JButton closeBtn = new JButton("Close");

        closeBtn.addActionListener(new ActionListener()
        {

            /**
             * Performs an action when close button is pressed
             * @param evt event to perform
             */
            @Override
            public void actionPerformed(ActionEvent evt)
            {
                setVisible(false);
            }
        });

        //CREATE tempPanel to hold a centered panel
        JPanel tempPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        tempPanel.add(closeBtn);
        panel.add(tempPanel);
    }
}
